package com.generator.common;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

/**
 * 自定义响应结构
 * 
 * @author admin
 *
 */
public class CommonResult {

	private static final String error_msg = "服务器开小差了，请稍后重试。";
	private static final String fail_msg = "程序操作出错，请稍后重试。";

	// 定义jackson对象
	private static final ObjectMapper MAPPER = new ObjectMapper();
	// 响应业务状态
	private Integer code;
	// 响应消息
	private String msg;
	// 响应中的数据
	private Object data;

	public static CommonResult build(Integer code, String msg, Object data) {
		return new CommonResult(code, msg, data);
	}

	public static CommonResult ok(Object data) {
		return new CommonResult(data);
	}

	public static CommonResult ok(String msg, Object data) {
		return new CommonResult(msg, data);
	}

	public static CommonResult ok() {
		return new CommonResult();
	}

	public CommonResult() {
		this.code = 200;
		this.msg = "OK";
		this.data = null;
	}

	public static CommonResult build(Integer code, String msg) {
		return new CommonResult(code, msg, null);
	}

	public static CommonResult failMsg(String fail_msg) {
		return new CommonResult(400, fail_msg, null);
	}

	public static CommonResult fail(Object data) {
		return new CommonResult(400, fail_msg, data);
	}
	public static CommonResult fail(String fail_msg, Object data) {
		return new CommonResult(400, fail_msg, data);
	}

	public static CommonResult errorMsg(String error_msg) {
		return new CommonResult(500, error_msg, null);
	}
	public static CommonResult error(Object data) {
		return new CommonResult(500, error_msg, data);
	}

	public static CommonResult error() {
		return new CommonResult(500, error_msg, null);
	}
	public static CommonResult error(String error_msg, Object data) {
		return new CommonResult(500, error_msg, data);
	}

	public CommonResult(Integer code, String msg, Object data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public CommonResult(String msg) {
		this.code = 200;
		this.msg = msg;
		this.data = null;
	}

	public CommonResult(Object data) {
		this.code = 200;
		this.msg = "OK";
		this.data = data;
	}

	public CommonResult(String msg, Object data) {
		this.code = 200;
		this.msg = msg;
		this.data = data;
	}

	// public Boolean isOK() {
	// return this.code == 200;
	// }

	public CommonResult(CodeMsg codeMsg) {
		this.code = codeMsg.getCode();
		this.msg = codeMsg.getMessage();
		this.data = null;
	}

	public CommonResult(CodeMsg codeMsg, Object obj) {
		this.code = codeMsg.getCode();
		this.msg = codeMsg.getMessage();
		this.data = obj;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	/**
	 * 将json结果集转化为CommonResult对象
	 * 
	 * @param jsonData
	 *            json数据
	 * @param clazz
	 *            CommonResult中的object类型
	 * @return
	 */
	public static CommonResult formatToPojo(String jsonData, Class<?> clazz) {
		try {
			if (clazz == null) {
				return MAPPER.readValue(jsonData, CommonResult.class);
			}
			JsonNode jsonNode = MAPPER.readTree(jsonData);
			JsonNode data = jsonNode.get("data");
			Object obj = null;
			if (clazz != null) {
				if (data.isObject()) {
					obj = MAPPER.readValue(data.traverse(), clazz);
				} else if (data.isTextual()) {
					obj = MAPPER.readValue(data.asText(), clazz);
				}
			}
			return build(jsonNode.get("code").intValue(), jsonNode.get("msg").asText(), obj);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 没有object对象的转化
	 * 
	 * @param json
	 * @return
	 */
	public static CommonResult format(String json) {
		try {
			return MAPPER.readValue(json, CommonResult.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Object是集合转化
	 * 
	 * @param jsonData
	 *            json数据
	 * @param clazz
	 *            集合中的类型
	 * @return
	 */
	public static CommonResult formatToList(String jsonData, Class<?> clazz) {
		try {
			JsonNode jsonNode = MAPPER.readTree(jsonData);
			JsonNode data = jsonNode.get("data");
			Object obj = null;
			if (data.isArray() && data.size() > 0) {
				obj = MAPPER.readValue(data.traverse(),
						MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
			}
			return build(jsonNode.get("code").intValue(), jsonNode.get("msg").asText(), obj);
		} catch (Exception e) {
			return null;
		}
	}

	public static CommonResult build(CodeMsg codeMsg) {
		return new CommonResult(codeMsg);
	}

	public static CommonResult build(CodeMsg codeMsg, Object obj) {
		return new CommonResult(codeMsg, obj);
	}

}
